package demo2;

import java.util.Stack;

//最小栈
class MinStack {
    Stack<Integer> stack1 = new Stack<>();
    Stack<Integer> stack2 = new Stack<>();

    //无参构造
    public MinStack() {
    }

    //入栈
    public void push(int val) {
        //判断栈是否为空
        if (isEmpty()) {
            stack1.push(val);
            stack2.push(val);
            return;
        }
        if (stack2.peek() >= val) {
            stack2.push(val);
        }
        stack1.push(val);
    }

    public void pop() {
        //判断栈是否为空
        if (isEmpty()) {
            throw new EmptyException("栈为空");
        }
        if (stack2.peek().equals(stack1.peek())) {
            stack2.pop();
        }
        stack1.pop();
    }

    public int top() {
        //判断栈是否为空
        if (isEmpty()) {
            throw new EmptyException("栈为空");
        }
        return stack1.peek();
    }

    public int getMin() {
        return stack2.peek();
    }

    //判断栈是否为空
    public boolean isEmpty() {
        return stack1.empty() && stack2.empty();
    }
}



public class Test2 {
    public static void main(String[] args) {
        MinStack minStack = new MinStack();
        minStack.push(-2);
        minStack.push(0);
        minStack.push(-3);
        System.out.println(minStack.getMin());
        minStack.pop();
        System.out.println(minStack.top());
        System.out.println(minStack.getMin());
    }
}

